package com.taobao.txc.a.b;

import com.taobao.txc.common.LoggerInit;
import com.taobao.txc.common.LoggerWrap;
import com.taobao.txc.common.b.c;
import com.taobao.txc.common.c.j;
import io.netty.channel.Channel;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public abstract class p
  extends ChannelDuplexHandler
{
  private static final LoggerWrap a = LoggerInit.logger;
  private String b = "DEFAULT";
  private final Object c = new Object();
  private final int d = 5000;
  protected volatile long i = 0L;
  protected final ThreadPoolExecutor j;
  protected com.taobao.txc.a.c.a k = new com.taobao.txc.a.c.a();
  protected boolean l = false;
  protected final ScheduledExecutorService m = Executors.newScheduledThreadPool(1);
  protected final ConcurrentHashMap<Long, b> n = new ConcurrentHashMap();
  protected final ConcurrentHashMap<String, BlockingQueue<u>> o = new ConcurrentHashMap();
  protected final ConcurrentHashMap<String, BlockingQueue<u>> p = new ConcurrentHashMap();
  protected final Object q = new Object();
  protected final Map<Long, com.taobao.txc.common.c.p> r = new ConcurrentHashMap();
  boolean s = false;
  
  public p(ThreadPoolExecutor paramThreadPoolExecutor)
  {
    this.j = paramThreadPoolExecutor;
  }
  
  public void b()
  {
    this.m.scheduleAtFixedRate(new q(this), 5000L, 5000L, TimeUnit.MILLISECONDS);
  }
  
  public void channelWritabilityChanged(ChannelHandlerContext paramChannelHandlerContext)
  {
    synchronized (this.c)
    {
      if (paramChannelHandlerContext.channel().isWritable()) {
        this.c.notify();
      }
    }
    paramChannelHandlerContext.fireChannelWritabilityChanged();
  }
  
  public Object a(String paramString, Channel paramChannel, Object paramObject, long paramLong)
  {
    return a(paramString, paramChannel, paramObject, paramLong, paramLong >= 0L);
  }
  
  public Object a(String paramString, Channel paramChannel, Object paramObject, long paramLong, boolean paramBoolean)
  {
    if (paramChannel == null)
    {
      a.warn("invoke nothing, caused by null channel.");
      return null;
    }
    u localu = new u();
    localu.a(u.a());
    localu.a(false);
    localu.c(false);
    localu.b(true);
    localu.a(paramObject);
    b localb = new b();
    localb.a(localu);
    localb.a(paramBoolean ? paramLong : 30000L);
    this.n.put(Long.valueOf(localu.e()), localb);
    Object localObject1;
    if ((paramString != null) && (!(paramObject instanceof j)) && (!(paramObject instanceof com.taobao.txc.common.c.t)))
    {
      localObject1 = null;
      if ((this instanceof g)) {
        localObject1 = this.o;
      } else {
        localObject1 = this.p;
      }
      BlockingQueue localBlockingQueue = (BlockingQueue)((ConcurrentHashMap)localObject1).get(paramString);
      if (localBlockingQueue == null)
      {
        ((ConcurrentHashMap)localObject1).putIfAbsent(paramString, new LinkedBlockingQueue());
        localBlockingQueue = (BlockingQueue)((ConcurrentHashMap)localObject1).get(paramString);
      }
      localBlockingQueue.offer(localu);
      if (a.c()) {
        a.debug("offer message: " + localu.f());
      }
      if (!this.l) {
        synchronized (this.q)
        {
          this.q.notify();
        }
      }
    }
    else
    {
      if (a.c()) {
        a.debug(String.format("%s wanted to send msgid:%s body:%s future:%s", new Object[] { this, Long.valueOf(localu.e()), localu.f(), localb }));
      }
      synchronized (this.c)
      {
        int i1 = 0;
        while (!paramChannel.isWritable()) {
          try
          {
            i1++;
            if (i1 > 3000)
            {
              try
              {
                paramChannel.disconnect();
                paramChannel.close();
                com.taobao.txc.common.g.a(paramChannel);
              }
              catch (Throwable localThrowable)
              {
                a.a("", "channel close error", localThrowable);
              }
              throw new c(paramChannel.toString() + " " + "channel is not writable" + ", msg:" + (paramObject == null ? "null" : paramObject.toString()));
            }
            this.c.wait(10L);
          }
          catch (InterruptedException localInterruptedException) {}
        }
      }
      localObject1 = paramChannel.writeAndFlush(localu);
      ((ChannelFuture)localObject1).addListener(new r(this, localu));
    }
    if (paramBoolean) {
      try
      {
        return localb.a(paramLong, TimeUnit.MILLISECONDS);
      }
      catch (Exception localException)
      {
        if (a.c()) {
          a.debug("messageFuture : " + localb);
        }
        throw new RuntimeException(localException + ",ip:" + paramString);
      }
    }
    return null;
  }
  
  protected void a(Channel paramChannel, Object paramObject)
  {
    u localu = new u();
    localu.a(true);
    localu.c(paramObject instanceof a);
    localu.b(true);
    localu.a(paramObject);
    localu.a(u.a());
    if ((paramObject instanceof com.taobao.txc.common.c.p)) {
      this.r.put(Long.valueOf(localu.e()), (com.taobao.txc.common.c.p)paramObject);
    }
    synchronized (this.c)
    {
      int i1 = 0;
      while (!paramChannel.isWritable()) {
        try
        {
          i1++;
          if (i1 > 3000)
          {
            try
            {
              paramChannel.disconnect();
              paramChannel.close();
              com.taobao.txc.common.g.a(paramChannel);
            }
            catch (Throwable localThrowable)
            {
              a.a("", "channel close error", localThrowable);
            }
            throw new c(paramChannel.toString() + " " + "channel is not writable" + ", msg:" + (paramObject == null ? "null" : paramObject.toString()));
          }
          this.c.wait(10L);
        }
        catch (InterruptedException localInterruptedException) {}
      }
    }
    if (a.c()) {
      a.debug("write message:" + localu.f() + ", channel:" + paramChannel + ",active?" + paramChannel.isActive() + ",writable?" + paramChannel.isWritable() + ",isopen?" + paramChannel.isOpen());
    }
    paramChannel.writeAndFlush(localu);
  }
  
  protected void a(long paramLong, Channel paramChannel, Object paramObject)
  {
    u localu = new u();
    localu.a(true);
    localu.c(paramObject instanceof a);
    localu.b(false);
    localu.a(paramObject);
    localu.a(paramLong);
    synchronized (this.c)
    {
      int i1 = 0;
      while (!paramChannel.isWritable()) {
        try
        {
          i1++;
          if (i1 > 3000)
          {
            try
            {
              paramChannel.disconnect();
              paramChannel.close();
              com.taobao.txc.common.g.a(paramChannel);
            }
            catch (Throwable localThrowable)
            {
              a.a("", "channel close error", localThrowable);
            }
            throw new c(paramChannel.toString() + " " + "channel is not writable" + ", msg:" + (paramObject == null ? "null" : paramObject.toString()));
          }
          this.c.wait(10L);
        }
        catch (InterruptedException localInterruptedException) {}
      }
    }
    if (a.c()) {
      a.debug("send response:" + localu.f() + ",channel:" + paramChannel);
    }
    paramChannel.writeAndFlush(localu);
  }
  
  public void channelRead(ChannelHandlerContext paramChannelHandlerContext, Object paramObject)
  {
    if ((paramObject instanceof u))
    {
      u localu = (u)paramObject;
      if (localu.c())
      {
        if (a.c()) {
          a.debug(String.format("%s msgId:%s, body:%s", new Object[] { this, Long.valueOf(localu.e()), localu.f() }));
        }
        try
        {
          this.j.execute(new s(this, localu, paramChannelHandlerContext));
        }
        catch (RejectedExecutionException localRejectedExecutionException1)
        {
          a.error(com.taobao.txc.common.b.b.e.bd, "thread pool is full, current max pool size is " + this.j.getActiveCount());
          if (this.s)
          {
            String str1 = ManagementFactory.getRuntimeMXBean().getName();
            String str2 = str1.split("@")[0];
            int i1 = new Random().nextInt(100);
            try
            {
              Runtime.getRuntime().exec("jstack " + str2 + " >d:/" + i1 + ".log");
            }
            catch (IOException localIOException)
            {
              localIOException.printStackTrace();
            }
            this.s = false;
          }
        }
      }
      else
      {
        b localb = (b)this.n.remove(Long.valueOf(localu.e()));
        if (a.c()) {
          a.debug(String.format("%s msgId:%s, future :%s, body:%s", new Object[] { this, Long.valueOf(localu.e()), localb, localu.f() }));
        }
        if (localb != null) {
          localb.a(localu.f());
        } else {
          try
          {
            this.j.execute(new t(this, localu, paramChannelHandlerContext));
          }
          catch (RejectedExecutionException localRejectedExecutionException2)
          {
            a.error(com.taobao.txc.common.b.b.e.bd, "thread pool is full, current max pool size is " + this.j.getActiveCount());
          }
        }
      }
    }
  }
  
  public void exceptionCaught(ChannelHandlerContext paramChannelHandlerContext, Throwable paramThrowable)
  {
    a.a(com.taobao.txc.common.b.b.ad.bd, paramChannelHandlerContext.channel() + " connect exception. " + paramThrowable.getMessage(), paramThrowable);
    try
    {
      paramChannelHandlerContext.disconnect();
      paramChannelHandlerContext.close();
      com.taobao.txc.common.g.a(paramChannelHandlerContext.channel());
    }
    catch (Exception localException)
    {
      a.a("", "close channel" + paramChannelHandlerContext.channel() + " fail.", localException);
    }
  }
  
  public abstract void a(long paramLong, ChannelHandlerContext paramChannelHandlerContext, Object paramObject);
  
  public void close(ChannelHandlerContext paramChannelHandlerContext, ChannelPromise paramChannelPromise)
  {
    a.info(paramChannelHandlerContext + " close");
    super.close(paramChannelHandlerContext, paramChannelPromise);
  }
  
  public String l()
  {
    return this.b;
  }
  
  public void h(String paramString)
  {
    this.b = paramString;
  }
}


/* Location:           C:\Users\ibm\Downloads\txc-yun-sample (1)\txc-yun-sample\lib\txc-client-2.0.72.jar
 * Qualified Name:     com.taobao.txc.a.b.p
 * JD-Core Version:    0.7.0.1
 */